package com.anbinh.doctor.fragment.benhnhan;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.DeseaseManifestionsAdapter;
import com.anbinh.doctor.model.DiseaseManifestations;
import com.anbinh.doctor.model.DiseaseManifestations.Manifestations;
import com.anbinh.doctor.model.ResponsePatient.Patient;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.PatientOptionType;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentListMedicalHistory extends Fragment {
	private static FragmentListMedicalHistory fragment = null;
	private View mView;
	private CircleProgressBar mLoading;
	private ListView mListview;
	private UserLogin userLogin;

	private List<Manifestations> mList = null;
	private DeseaseManifestionsAdapter adapter;
	private int currentPage = 0;
	private View fotter;
	protected Manifestations currentBieuhien;

	public static FragmentListMedicalHistory newInstance() {
		if (fragment == null) {
			fragment = new FragmentListMedicalHistory();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_list_images, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mListview = (ListView) mView.findViewById(R.id.listDrag);
		mListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				currentBieuhien = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, R.id.them_lich_su_kham);
				bundle.putBoolean(Utils.CUSTOMBAR, true);
				bundle.putBoolean(Utils.KEY_UPDATE, true);
				bundle.putInt(Utils.REQUEST_CODE, Utils.ONRESUME_BIEU_HIEN);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.sua_lich_su_kham_chua_benh));
				Utils.getNewIntent(getActivity(), bundle);

			}

		});

		mLoading = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadData();

	}

	public Manifestations getCurrentBieuhien() {
		return currentBieuhien;
	}

	public void onLoadData() {
		Utils.onLoadding(mView, true);
		Patient patient = FragmentListPatients.newInstance()
				.getCurrentPatient();

		ServerController.getInstance(getActivity())
				.requestlistMedicalHistory(userLogin.getData().getToken(),

				patient.getId(), FragmentListPatients.newInstance().getCurrentDoctorId(),
						new OnRequest<DiseaseManifestations>() {
							@Override
							public void onSuccess(DiseaseManifestations data) {
								if (getActivity() == null)
									return;

								mLoading.setVisibility(View.INVISIBLE);
								Utils.disableEnableControls(true, mView);

								if (data.getResponsecode() >= 0
										&& data.getManifestations() != null
										&& data.getManifestations().size() > 0) {

									mList = data.getManifestations();
									adapter = new DeseaseManifestionsAdapter(
											getActivity(),
											R.layout.item_bieuhien, mList, PatientOptionType.LICH_SU_KHAM_CHUA_BENH.getCode());
									mListview.setAdapter(adapter);
								}

							}

							@Override
							public void onError(int errorCode, String message) {
								mLoading.setVisibility(View.INVISIBLE);
								Utils.disableEnableControls(true, mView);
							}
						});
	}
}
